package com.guigu.util.service.impl;

import com.guigu.util.mapper.IDMapper;
import com.guigu.util.pojo.ID;
import com.guigu.util.service.IDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class IDServiceImpl implements IDService {

    @Autowired
    IDMapper idMapper;

    // 通用方法
    public  Map<String,Object> FangFa(Integer iid, String ID){
        Date date = new Date();
        int nian = 1900 + date.getYear();
        int yue = date.getMonth() + 1;
        int ri = date.getDate();
        if (yue==1 && ri==1){
            iid=1;
        }
        ID += "" + nian + (yue > 10 ? "0" + yue : yue) + (ri > 10 ? "0" + ri : ri);
        String mdId = iid + "";
        int l = mdId.length();
        switch (l) {
            case 1:
                ID += "000" + mdId;
                break;
            case 2:
                ID += "00" + mdId;
                break;
            case 3:
                ID += "0" + mdId;
                break;
            case 4:
                ID += "" + mdId;
                break;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("strID",ID);
        map.put("Iid",iid+1);
        return map;
    }

    // 产品编号
    public String ProductID(String I, String II, String III) {
        int iid = (idMapper.select()).getProductID();
        String ID = "100";
        ID += I.substring(0, 2);
        ID += II.substring(0, 2);
        ID += III.substring(0, 2);
        String pId = iid + "";
        int l = pId.length();
        switch (l) {
            case 1:
                ID += "00000" + pId;
                break;
            case 2:
                ID += "0000" + pId;
                break;
            case 3:
                ID += "000" + pId;
                break;
            case 4:
                ID += "00" + pId;
                break;
            case 5:
                ID += "0" + pId;
                break;
            case 6:
                ID += "" + pId;
                break;
        }
        ID dui = new ID();
        dui.setProductID(iid + 1);
        idMapper.update(dui);
        return ID;
    }
    // 物料设计单编号
    public String EbomID() {
        int iid = (idMapper.select()).getEbomID();
        String ID = "200";

        Map<String, Object> map = FangFa(iid, ID);

        ID dui = new ID();
        dui.setEbomID((Integer) map.get("Iid"));
        idMapper.update(dui);
        return (String) map.get("strID");
    }
    // 工序设计单编号
    public String PDS_ID() {
        int iid = (idMapper.select()).getPdsID();
        String ID = "201";

        Map<String, Object> map = FangFa(iid, ID);

        ID dui = new ID();
        dui.setPdsID((Integer) map.get("Iid"));
        idMapper.update(dui);
        return (String) map.get("strID");
    }
    // 生产计划编号
    public String PMC_ID() {
        int iid = (idMapper.select()).getPmcID();
        String ID = "300";

        Map<String, Object> map = FangFa(iid, ID);

        ID dui = new ID();
        dui.setPmcID((Integer) map.get("Iid"));
        idMapper.update(dui);
        return (String) map.get("strID");
    }
    // 派工单编号
    public String Work_OrDer_ID() {
        int iid = (idMapper.select()).getWorkorderID();
        String ID = "301";

        Map<String, Object> map = FangFa(iid, ID);

        ID dui = new ID();
        dui.setWorkorderID((Integer) map.get("Iid"));
        idMapper.update(dui);
        return (String) map.get("strID");
    }
    // 库存编号
    public String Repertory_ID() {
        int iid = (idMapper.select()).getRepertoryID();
        String ID = "400";

        Map<String, Object> map = FangFa(iid, ID);

        ID dui = new ID();
        dui.setRepertoryID((Integer) map.get("Iid"));
        idMapper.update(dui);
        return (String) map.get("strID");
    }
    // 入库单编号
    public String Stock_In_ID() {
        int iid = (idMapper.select()).getStockInID();
        String ID = "401";

        Map<String, Object> map = FangFa(iid, ID);

        ID dui = new ID();
        dui.setStockInID((Integer) map.get("Iid"));
        idMapper.update(dui);
        return (String) map.get("strID");
    }
    // 出库单编号
    public String Stock_Out_ID() {
        int iid = (idMapper.select()).getStockOutID();
        String ID = "402";

        Map<String, Object> map = FangFa(iid, ID);

        ID dui = new ID();
        dui.setStockOutID((Integer) map.get("Iid"));
        idMapper.update(dui);
        return (String) map.get("strID");
    }
}
